# coding: UTF-8

import urllib
from google.appengine.api import urlfetch
from django.utils import simplejson
import logging

class LocalSearch:


    def __init__(self, appid, datum='wgs'):
        self.appid = appid
        self.datum = 'wgs'
        self.url = 'http://map.yahooapis.jp/LocalSearchService/V1/LocalSearch'

    #category =address, zipcode, landmark, station, courpon
    #al = 1 - 市区町村レベル
    #     2 - 町、大字レベル
    #     3 - 丁目、字レベル
    def search_query(self, query, result=10, start=1, category='', address_level=3):
        url = self.url
        url += '?' + urllib.urlencode( {
            'appid'     : self.appid ,
            'datum'     : self.datum ,
            'o'         : 'json',
            'p'         : query.encode('utf-8'),
            'category'  : category,
            'al'        : address_level,
            'b'         : start,
            'n'         : result,
        })

        logging.info(url)
        result = urlfetch.fetch( url , method=urlfetch.GET)
        return simplejson.loads( result.content )       
 
